/**
 * 
 */
package de.cbf.cam.dsa.facet.talent.helper.editing_support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import de.cbf.cam.dsa.facet.talent.FacetTalent;
import de.cbf.cam.dsa.facet.talent.helper.CompensationTalentReference;
import de.cbf.cam.dsa.facet.talent.test_support.ConstantsSWTBOTTalent;
import de.cbf.util.db.interfaces.exceptions.EntityLoadException;
import de.cbf.util.entity.edit.category_tree.dialogs.DialogChooseEntity;
import de.cbf.util.entity.edit.category_tree.helper.UUIDString;
import de.cbf.util.entity.facet.entities.EntityCategorized;
import de.cbf.util.entity.facet.entities.EntityFacetReferences;
import de.cbf.util.entity.facet.persistence.resolver.IServiceResolverEntity;
import de.cbf.util.ui.helper.ICustomizableEditingSupport;
import de.cbf.util.ui.test_support.ConstantsSWTBOTUtil;

/**
 * @author Nebelritter
 *
 */
public class CompensationTalentEditingSupport extends EditingSupport implements ICustomizableEditingSupport{
	private static final Log logger = LogFactory.getLog(CompensationTalentEditingSupport.class);

	public static final String DIALOG_TITLE_CHOOSE_TALENT = "Choose Talent..";

	public static final String TEXT_SECTION_TALENTS = "Talents by Category"; 
	
	
	protected TableViewer tableViewer;

	protected IServiceResolverEntity serviceTalentResolver;

	/**
	 * 
	 */
	public CompensationTalentEditingSupport(ColumnViewer viewer) {
		super(viewer);		
		if (viewer instanceof TableViewer) {
			TableViewer tableViewer = (TableViewer)viewer;
			this.tableViewer = tableViewer;
		}else{
			String err_msg = "entity of wrong type in CompensationTalentEditingSupport : \n"+
					"Expected: "+TableViewer.class.getSimpleName()+"\n"+ 
					"Got: "+ (viewer == null ? "null" : viewer.getClass().getSimpleName());
			logger.error(err_msg);
		}
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		DialogCellEditor cellEditor = new DialogCellEditor(tableViewer.getTable()) {

			protected UUIDString currentUuidString;

			@Override
			protected Object openDialogBox(Control cellEditorWindow) {
				DialogChooseEntity dialog = new DialogChooseEntity(cellEditorWindow.getShell());
				//customize dialog
				dialog.setDialogTitle(DIALOG_TITLE_CHOOSE_TALENT);
				dialog.setSectionTitle(TEXT_SECTION_TALENTS);
				dialog.setActiveBaseFacet(FacetTalent.class);
				dialog.setUUIDForUncategorized(EntityCategorized.CATEGORY_UNCATEGORIZED_UUID_DEFAULT);
				dialog.setNameForUncategorized(EntityCategorized.CATEGORY_UNCATEGORIZED_NAME_DEFAULT);				
				try {					
					Collection<EntityFacetReferences> elements = serviceTalentResolver.resolveAllEntities();					
					dialog.setEntityStructure(new ArrayList<EntityCategorized>(elements));
				} catch (EntityLoadException e) {
					logger.error(e);					
				}
				//pre set value
				dialog.setValue(currentUuidString);
				int dialogResult = dialog.open();
				if(dialogResult == DialogChooseEntity.OK){
					return dialog.getValue();
				}
				//if cancelled return value that was already there
				return currentUuidString;
			}

			
			
			/* (non-Javadoc)
			 * @see org.eclipse.jface.viewers.DialogCellEditor#createContents(org.eclipse.swt.widgets.Composite)
			 */
			@Override
			protected Control createContents(Composite cell) {
				// how cell should look, base implementation is a label
				return super.createContents(cell);
			}

			/* (non-Javadoc)
			 * @see org.eclipse.jface.viewers.DialogCellEditor#updateContents(java.lang.Object)
			 */
			@Override
			protected void updateContents(Object value) {// update the content of the CELL !
				if(value == null) return;//value will be null in initializing
				if (value instanceof UUIDString) {
					currentUuidString = (UUIDString)value;					
					super.updateContents(currentUuidString.string);//set the default label to text of reference
				}else{
					String err_msg = "entity of wrong type in updateContents : \n"+
							"Expected: "+UUIDString.class.getSimpleName()+"\n"+ 
							"Got: "+ (value == null ? "null" : value.getClass().getSimpleName());
					logger.error(err_msg);
				}					
			}

			/* (non-Javadoc)
			 * @see org.eclipse.jface.viewers.DialogCellEditor#createButton(org.eclipse.swt.widgets.Composite)
			 */
			@Override
			protected Button createButton(Composite parent) {
				//overridden to provide id data for test with SWTBOT
				Button buttonInCell = super.createButton(parent);
				buttonInCell.setData(ConstantsSWTBOTUtil.DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_COMPOSITE_COMPENSATION_TALENTS_CELL_BUTTON_ID);
				return buttonInCell;
			}			
		};
		return cellEditor;
	}

	@Override
	protected boolean canEdit(Object element) {		
		return true;
	}

	protected Object getValue(Object element) {
		if (element instanceof CompensationTalentReference) {
			CompensationTalentReference compensationTalentRef = (CompensationTalentReference)element;
			UUID savedUUID = compensationTalentRef.getSavedUuid();
			String savedName = compensationTalentRef.getSavedName();
			UUIDString value = new UUIDString(savedUUID,savedName);			
			return value;
		}else{
			String err_msg = "entity of wrong type in getValue : \n"+
					"Expected: "+CompensationTalentReference.class.getSimpleName()+"\n"+ 
					"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
			logger.error(err_msg);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#setValue(java.lang.Object, java.lang.Object)
	 */
	@Override
	protected void setValue(Object element, Object value) {
		if (element instanceof CompensationTalentReference && value instanceof UUIDString) {
			CompensationTalentReference compensationTalentRef = (CompensationTalentReference)element;
			UUIDString newUUIDString = (UUIDString)value;
			compensationTalentRef.setSavedUuid(newUUIDString.uuid);
			compensationTalentRef.setSavedName(newUUIDString.string);
			tableViewer.update(element, null);		
		}else{
			String err_msg = "entity of wrong type in setValue : \n"+
					"Expected: "+CompensationTalentReference.class.getSimpleName()+"\n"+ 
					"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
			logger.error(err_msg);
		};
	}
	
	public void customize(Object obj){
		if (obj instanceof IServiceResolverEntity) {
			IServiceResolverEntity serviceResolverEntity = (IServiceResolverEntity) obj;
			this.serviceTalentResolver = serviceResolverEntity;
		} else {
			String err_msg = "entity of wrong type in customize : \n"
					+ "Expected: " + IServiceResolverEntity.class.getSimpleName() + "\n"
					+ "Got: "+ (obj == null ? "null" : obj.getClass().getSimpleName());
			logger.error(err_msg);
		}
	}
}
